package com.example.demo.repository;


import com.example.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
    @Query(value = "SELECT * FROM user WHERE Username = %?1  AND  Password = %?2;",nativeQuery = true)
    User findByUsemameAndPassword(String username, String password);

    @Query(value = "SELECT * FROM user WHERE Phone = %?1  OR  Email = %?2;",nativeQuery = true)
    User findByPhoneOrEmail(String phone,  String email);

    @Query(value = "SELECT * FROM user WHERE username = %?1  ;",nativeQuery = true)
    List<User> findByName(String keystring);
    /*
    @Query(value = "SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS result FROM user WHERE Phone LIKE %?1;",nativeQuery = true)
    boolean existsByPhone(String phone);

    @Query(value = "SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS result FROM user WHERE Email LIKE %?1;",nativeQuery = true)
    boolean existsByEmail(String email);
     */


    @Query(value = "SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS result FROM user WHERE Phone LIKE %?1;",nativeQuery = true)
    boolean existsByPhone(String phone);

    @Query(value = "SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END AS result FROM user WHERE Email LIKE %?1;",nativeQuery = true)
    boolean existsByEmail(String email);


}
